package fun.sanguo.service.sys.impl;

import fun.sanguo.mapper.sys.SysVersionMapper;
import fun.sanguo.model.dto.DataInfo;
import fun.sanguo.model.dto.sys.SysVersionResult;
import fun.sanguo.model.entity.sys.SysVersionEntity;
import fun.sanguo.model.enums.ResEnums;
import org.springframework.stereotype.Service;
import fun.sanguo.service.sys.SysVersionService;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统-版本信息 Service实现类
 *
 * @author ZYP
 * @date 2022-10-18
 */
@Service
public class SysVersionServiceImpl implements SysVersionService {

    @Resource
    private SysVersionMapper sysVersionMapper;

    /**
     * 查询最新版本
     *
     * @return
     * @throws ArtificialException
     */
    @Override
    public DataInfo<SysVersionResult> getLastVersion() throws ArtificialException {
        // 查询数据并封装返回
        try {
            // 查询最新版本
            SysVersionEntity sysVersionEntity = sysVersionMapper.getLastVersion();
            // 返回结果
            SysVersionResult sysVersionResult = new SysVersionResult();
            // 转化数据
            sysVersionResult.setVersion(sysVersionEntity.getReleaseVersion());
            sysVersionResult.setDate(sysVersionEntity.getReleaseDate());
            sysVersionResult.setNote(sysVersionEntity.getReleaseNote());
            // 封装并返回数据信息
            return new DataInfo<>(sysVersionResult);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 查询版本记录
     *
     * @return
     * @throws ArtificialException
     */
    @Override
    public DataInfo<List<SysVersionResult>> getReleaseHistory() throws ArtificialException {
        // 查询数据并封装返回
        try {
            // 查询版本记录
            List<SysVersionEntity> sysVersionEntityList = sysVersionMapper.getReleaseHistory();
            // 返回结果
            List<SysVersionResult> sysVersionResultList = new ArrayList<>();
            // 遍历转化数据
            sysVersionEntityList.stream().forEach(sysVersionEntity -> {
                SysVersionResult sysVersionResult = new SysVersionResult();
                sysVersionResult.setVersion(sysVersionEntity.getReleaseVersion());
                sysVersionResult.setDate(sysVersionEntity.getReleaseDate());
                sysVersionResult.setNote(sysVersionEntity.getReleaseNote());
                sysVersionResultList.add(sysVersionResult);
            });
            // 封装并返回数据信息
            return new DataInfo<>(sysVersionResultList);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }
}
